const app = getApp()
import areaList from '../../config/area.js';
import {
  homeInfo,
  whetherIf
} from '../../config/https'
//首页数据、判断审核状态
Page({

  /**
   * 页面的初始数据
   */
  data: {
    masterTit: '师傅入驻',
    mastertips: '填写入驻资料',
    chkLocation: false,
    maskShow: false,
    areaList: areaList,
    city: '请选择',
    adcode: '',
    indexData: ''
  },
  goQuestion() {
    wx.navigateTo({
      url: '/pages/questionList/questionList',
    })
  },
  //省市弹窗
  getProvince() {
    this.setData({
      adcode: app.globalData.adcode,
      maskShow: true
    })
  },
  //关闭省市弹窗
  onClose() {
    this.setData({
      maskShow: false
    })
  },
  //获取省市
  confirmClick(e) {
    let city = e.detail.values[1].name
    let selectVal = e.detail.values[0].name + e.detail.values[1].name
    this.setData({
      city
    })
    app.getcoordinate(selectVal).then(res => {
      console.log(res)
      this.setData({
        longitude: res.location.lng, //经度
        latitude: res.location.lat //纬度
      })
      app.globalData.address = res.address_components.province + res.address_components.city
      app.globalData.province = res.address_components.province
      app.globalData.city = res.address_components.city
      app.globalData.adcode = res.ad_info.adcode
      app.globalData.longitude = res.location.lng
      app.globalData.latitude = res.location.lat
      this.getData()
    })
    this.onClose()
  },
  // 师傅入驻
  goSettled() {
    if (!wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/logs/logs',
      })
      return
    }
    whetherIf({
      token: wx.getStorageSync('token')
    }).then(res => {
      // msg: 0 审核中 1 审核通过 2：审核失败 3: 招募师傅
      if (res.code == 1) {
        if (res.msg == 0 || res.msg == 1 || res.msg == 2) {
          if (res.msg == 1) {
            wx.navigateTo({
              url: '/pages/myState/myState',
            })
            return
          }
          wx.navigateTo({
            url: '/pages/examine/examine?type=' + res.msg
          })
        } else if (res.msg == 3) {
          wx.navigateTo({ // 招募师傅
            url: '/pages/recruit/recruit'
          })
        }
      }
    })
  },
  ifMaster() {
    whetherIf({
      token: wx.getStorageSync('token')
    }).then(res => {
      // msg: 0 审核中 1 审核通过 2：审核失败
      if (res.code == 1) {
        if (res.msg == 1) {
          this.setData({
            masterTit: '我的状态',
            mastertips: '查看最新状态'
          })
        }
      }
    })
  },
  // 跳转搜索师傅
  goSearch() {
    wx.navigateTo({
      url: '/pages/searchMaster/searchMaster',
    })
  },
  // 跳转案例详情
  goCaseDetails(e) {
    let {
      orderid,
      userid
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/caseDetails/caseDetails?orderid=' + orderid + "&uid=" + userid,
    })
  },
  // 跳转同城师傅
  SameCity(e) {
    wx.navigateTo({
      url: '/pages/sameCity/sameCity?masterType=' + e.currentTarget.dataset.type,
    })
  },
  // 跳转找师傅
  lookMaster() {
    wx.switchTab({
      url: '/pages/masterWorker/masterWorker',
    })
  },
  // 跳转师傅详情
  masterDetails(e) {
    wx.navigateTo({
      url: '/pages/workDetails/workDetails?id=' + e.currentTarget.dataset.uid,
    })
  },
  getData() {
    homeInfo({
      address: this.data.address ? this.data.address : ''
    }).then(res => {
      if (res.code == 1) {
        this.setData({
          indexData: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!app.globalData.chkLocation) {
      app.location().then(res => {
        this.setData({
          city: res.address_component.city,
          address: res.address_component.province + res.address_component.city
        })
        this.getData()
      }).catch(err => {
        this.getData()
        console.log(err)
      })
    } else {
      this.getData()
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('token')) {
      this.ifMaster()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})